// pages/data/data.js
const wxCharts = require('../../../util/wxcharts.js'); // 引入wx-charts.js文件
//定义记录初始屏幕宽度比例，便于初始化
var windowW = 0;
const util = require("../../../util/util.js");
var app = getApp();//引用app.js
var commonURL = app.data.baseURL;
var commonURLYY = app.data.newURL;
var commonURLXX = app.data.lastURL;
var baseURL1 = app.data.baseURL1;
var jielong = app.data.jielong;
Page({
  data: {
    nav: ['今天', '最近7天', '历史数据'],
    selectIndex: 0,
    listData: [],
    issend: false,
    startDate: "2018-11-24",
    endDate: "2018-11-25",
  },
  onLoad: function (options) {
    var that = this;
    this.setData({
      imageWidth: wx.getSystemInfoSync().windowWidth
    });
    var start_time = util.formatTime(new Date());
    var end_time = util.getDateStr(null, 1);
    this.setData({
      start_time: start_time,
      end_time: end_time
    })
    //计算屏幕宽度比列
    windowW = this.data.imageWidth / 375;
    console.log(windowW);
    console.log(new Date())
  },
  bindendDate: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      endDate: e.detail.value
    })
  },
  bindstartDate: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      startDate: e.detail.value
    })
  },
  search: function () {
    this.getData(this.data.startDate, this.data.endDate)
  },
  //下载报表
  download: function (e) {
    wx.showToast({
      title: '暂不支持！',
      icon: 'none',
      duration: 4000
    })
    // console.log(e)
    // this.setData({
    //   issend: true
    // })
  },
  onReady: function () {

  },
  //选项卡
  selectNav: function (e) {
    this.setData({
      selectIndex: e.currentTarget.dataset.index
    })
    if (e.currentTarget.dataset.index == 1) {
      var end_time = util.formatTime(new Date());
      var start_time = util.getDateStr(null, -6);
      var categories = []
      categories.push(start_time)
      categories.push(end_time)
      console.log(categories)
      this.getData(start_time, end_time)
    }
    if (e.currentTarget.dataset.index == 0) {
      var categories = []
      categories.push(this.data.start_time)
      categories.push(this.data.end_time)
      console.log(categories)
      this.getData(this.data.start_time, this.data.end_time, categories)
    }
  },
  cancel: function () {
    this.setData({
      issend: false
    })
  },
  confirm: function () {
    this.setData({
      issend: false
    })
  },
  getData: function (start_time, end_time, categories) {
    var that = this;
    wx.request({
      url: jielong + 'action=getOrderData',
      method: 'POST',
      data: {
        uniacid: app.data.GZHid,
        start_time: start_time,
        end_time: end_time,
        unionId: app.data.unionId || wx.getStorageSync('unionId'),
        openid: wx.getStorageSync('openid')
      },
      success: function (res) {
        console.log(res)
        var listData = {}
        var turnover = []
        var Refunds = []
        var listData = [];
        var totalturnover = 0
        var totalRefunds = 0
        for (var i = 0; i < res.data.data.day.length; i++) {
          var a = res.data.data.data[i][0]
          var b = res.data.data.data[i][1]
          turnover.push(res.data.data.data[i][0])
          Refunds.push(res.data.data.data[i][1])
          totalturnover += res.data.data.data[i][0]
          totalRefunds += res.data.data.data[i][1]
          if (that.data.selectIndex == 0){
            var tem = {
              "date": util.formatTime(new Date()),
              "turnover": a,
              "Refunds": b,
            }
            listData.push(tem)
          }
          that.setData({
            listData: listData
          })
        }
        
        if (that.data.selectIndex == 1 || that.data.selectIndex == 2) {
          var tem = {
            "date": start_time + '-' + end_time,
            "turnover": totalturnover.toFixed(3),
            "Refunds": totalRefunds.toFixed(3),
          }
          listData.push(tem)
        }
        that.setData({
          listData: listData,
          turnover: turnover,
          Refunds: Refunds,
          totalRefunds: totalRefunds.toFixed(3),
          totalturnover: totalturnover.toFixed(3)
        })
        
        if (categories) {
          var categories1 = categories
        } else {
          categories1 = res.data.data.day
        }
        new wxCharts({
          canvasId: 'columnCanvas',
          type: 'column',
          animation: true,
          categories: categories1,
          series: [{
            name: '营业额',
            data: turnover,
            format: function (val, name) {
              console.log(val)
              if (val == 0) {
                return val = 0
              } else {
                return val.toFixed(2);
              }

            }
          }, {
            name: '退款额',
            data: Refunds,
            format: function (val, name) {
              console.log(val)
              if (val == 0) {
                return val = 0
              } else {
                return val.toFixed(2);
              }
            }
          }],
          yAxis: {
            format: function (val) {
              return val;
            },
            title: '金额',
            min: 0
          },
          xAxis: {
            disableGrid: false,
            type: 'calibration'
          },
          extra: {
            column: {
              width: 30
            }
          },
          width: (375 * windowW),
          height: (200 * windowW),
        });
      }
    })
  },
  onShow: function () {
    var that = this;
    var categories = []
    categories.push(this.data.start_time)
    categories.push(this.data.end_time)
    console.log(categories)
    this.getData(this.data.start_time, this.data.end_time, categories)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

})